package com.module.controller;


import com.module.framework.domain.Comment;
import com.module.framework.domain.CommentList;
import com.module.framework.domain.ResponseResult;
import com.module.framework.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 获取评论列表
    @GetMapping("/list")
    public ResponseResult commentList(Integer pageNum, Integer pageSize) {
        List<List<Comment>> pageVO = commentService.commentList(pageNum, pageSize);
        return ResponseResult.okResult(pageVO);
    }

    // 获取单个评论的详细信息
    @GetMapping("/info")
    public ResponseResult getCommentInfo(Integer id) {
        CommentList comment = commentService.getCommentInfo(id);
        return ResponseResult.okResult(comment);
    }

    // 添加评论
    @GetMapping("/add")
    public ResponseResult addUser(Integer bookId, String comment) {
        commentService.addComment(bookId, comment);
        return ResponseResult.okResult();
    }

    // 删除评论接口
    @DeleteMapping("/delete")
    public ResponseResult deleteComment(Integer id) {
        try {
            System.out.println("Received ID in controller: " + id);
            boolean result = commentService.deleteComment(id);
            if (result) {
                return ResponseResult.okResult("评论删除成功");
            } else {
                return ResponseResult.errorResult(500, "评论删除失败");
            }
        } catch (Exception e) {
            return ResponseResult.errorResult(500, "删除操作出现异常：" + e.getMessage());
        }
    }


}

